package huang.course.feb03;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class TsetJsp
 */
public class TsetJsp extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TsetJsp() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		System.out.println("do get:"+request.getParameter("getvalue"));
		//request.setAttribute(arg0, arg1);
		//九大类jsp内置对象 1.request范围：用户当前提交的请求
		
		System.out.println("request.getContextPath()="+request.getContextPath());

		System.out.println("request.getRemoteAddr()="+request.getRemoteAddr());
		
		System.out.println("request.getRequestedSessionId()="+request.getRequestedSessionId());
		
		System.out.println("request.getProtocol()="+request.getProtocol());
	
		System.out.println("request.getRemoteHost()="+request.getRemoteHost());
		
		//2.response 对客户端的相应。向客户端发送数据。
		
		response.getWriter().append("登陆成功！");
		//重定向的方式做页面跳转。相当于，直接让浏览器访问另外一个地址
		response.sendRedirect("");
		//请求转发的方式做跳转页面.相当于，浏览器的地址不变，只是浏览器加载了内容，变成了新的内容的地址
		request.getRequestDispatcher("").forward(request, response);
		//3.session :只要客户不关闭浏览器，session里面存的值就一直存在。
		HttpSession as=request.getSession();
		String name="cx";
		as.setAttribute("name", name);
		//4.application 服务器启动，就会创建一个application对象，只要服务器没有关闭，里面存的值就一直在。
		//ServletContext application =this.getServletContext();
		
		//application.setAttribute("test", 100);
		//5.Out 向客户端发送数据。
		PrintWriter out =response.getWriter();
		out.println("com from out.print");
		//6.cookie 
		Cookie ck=new Cookie("key","value");
		int week=1000*60*60*24*7;
		ck.setMaxAge(week);
		response.addCookie(ck);
		//7.config
		//8.page(PageContext);
		
		//9.Exception
		
		//四大作用域
		
		//1 page,里面存的值只能当前页面，只要发生页面跳转，里面值就没了。
		//2 request，里面存的值可以跨越forward的前后两个页面。只要刷新页面有没有了。
		//3 session，只要不关闭浏览器，里面春的值就在，一旦关闭浏览器，里面的值就不存在。
		//4 application，只要不关闭服务器，里面存的值就一直在，一旦关闭服务器，里面存的值就不存在。
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
